package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.pojo.entity.PublishInfo;
import com.itheima.dubbo.interfaces.PublishInfoApi;
import com.itheima.dubbo.mysql.mapper.PublishInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/18
 * @Version 1.0
 * @describe
 */
@Service
public class PublishInfoApiImpl implements PublishInfoApi {

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    /**
     * 保存审核后信息
     *
     * @param publishInfo
     */
    @Override
    public void savePublishInfo(PublishInfo publishInfo) {
        publishInfoMapper.insert(publishInfo);
    }

    /**
     * 查询审核通过的个人圈子信息及排序
     *
     * @param page      当前页码
     * @param pagesize  每页页数
     * @param sortProp  排序字段 reportCount likeCount commentCount forwardingCount 默认createDate
     * @param sortOrder 排序规则 ascending 默认descending
     * @param userId    用户id
     * @return
     */
    @Override
    public List<PublishInfo> queryAllPublishByUserId(Integer pagesize, Integer page, String userId, String sortProp, String sortOrder) {

        // 默认审核通过，置顶降序
        LambdaQueryWrapper<PublishInfo> queryWrapper = Wrappers.lambdaQuery(PublishInfo.class)
                .eq(PublishInfo::getUserId, userId)
                .in(PublishInfo::getState, "2", "5")
                .orderByDesc(PublishInfo::getIstop);

        switch (sortProp) {
            case "reportCount":
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), PublishInfo::getReportCount);
                break;
            case "likeCount":
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), PublishInfo::getLikeCount);
                break;
            case "commentCount":
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), PublishInfo::getCommentCount);
                break;
            case "forwardingCount":
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), PublishInfo::getForwardingCount);
                break;
            default:
                queryWrapper = queryWrapper.orderBy(true, "ascending".equals(sortOrder), PublishInfo::getCreated);
                break;
        }

        Page<PublishInfo> publishInfoPage = publishInfoMapper.selectPage(new Page<>(page, pagesize), queryWrapper);
        return publishInfoPage.getRecords();
    }

    /**
     * 分页正序查询通过 2 5
     *
     * @param pagesize
     * @param page
     * @param likeId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<PublishInfo> queryPassPublishInfo(Integer pagesize, Integer page, Integer likeId, String startDate, String endDate) {
        Page<PublishInfo> publishInfoPage = publishInfoMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(PublishInfo.class)
                        .like(ObjectUtil.isNotNull(likeId), PublishInfo::getUserId, likeId)
                        .gt(!StrUtil.equals("-1", startDate), PublishInfo::getCreated, Convert.toLong(startDate))
                        .lt(!StrUtil.equals("-1", endDate), PublishInfo::getCreated, Convert.toLong(endDate))
                        .in(PublishInfo::getState, "2", "5")
                        .orderByAsc(PublishInfo::getCreated));
        return publishInfoPage.getRecords();
    }

    /**
     * 倒序查全部
     *
     * @param pagesize
     * @param page
     * @param likeId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<PublishInfo> queryAllPublishInfo(Integer pagesize, Integer page, Integer likeId, String startDate, String endDate) {
        Page<PublishInfo> publishInfoPage = publishInfoMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(PublishInfo.class)
                        .like(ObjectUtil.isNotNull(likeId), PublishInfo::getUserId, likeId)
                        .gt(!StrUtil.equals("-1", startDate), PublishInfo::getCreated, Convert.toLong(startDate))
                        .lt(!StrUtil.equals("-1", endDate), PublishInfo::getCreated, Convert.toLong(endDate))
                        .orderByDesc(PublishInfo::getCreated));
        return publishInfoPage.getRecords();
    }

    /**
     * 倒序查待审核 1 3
     *
     * @param pagesize
     * @param page
     * @param likeId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<PublishInfo> queryWaitToCheck(Integer pagesize, Integer page, Integer likeId, String startDate, String endDate) {
        Page<PublishInfo> publishInfoPage = publishInfoMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(PublishInfo.class)
                        .like(ObjectUtil.isNotNull(likeId), PublishInfo::getUserId, likeId)
                        .gt(!StrUtil.equals("-1", startDate), PublishInfo::getCreated, Convert.toLong(startDate))
                        .lt(!StrUtil.equals("-1", endDate), PublishInfo::getCreated, Convert.toLong(endDate))
                        .in(PublishInfo::getState, "1", "3")
                        .orderByDesc(PublishInfo::getCreated));
        return publishInfoPage.getRecords();
    }

    /**
     * 倒序查已拒绝 4 6
     *
     * @param pagesize
     * @param page
     * @param likeId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<PublishInfo> queryRefusePublishInfo(Integer pagesize, Integer page, Integer likeId, String startDate, String endDate) {
        Page<PublishInfo> publishInfoPage = publishInfoMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(PublishInfo.class)
                        .like(ObjectUtil.isNotNull(likeId), PublishInfo::getUserId, likeId)
                        .gt(!StrUtil.equals("-1", startDate), PublishInfo::getCreated, Convert.toLong(startDate))
                        .lt(!StrUtil.equals("-1", endDate), PublishInfo::getCreated, Convert.toLong(endDate))
                        .in(PublishInfo::getState, "4", "6")
                        .orderByDesc(PublishInfo::getCreated));
        return publishInfoPage.getRecords();
    }

    /**
     * 计数该用户所有审核通过的消息
     *
     * @param userId
     * @return
     */
    @Override
    public Integer countAllPublishByUserId(String userId) {
        return publishInfoMapper.selectCount(Wrappers.lambdaQuery(PublishInfo.class)
                .eq(PublishInfo::getUserId, userId)
                .in(PublishInfo::getState, "2", "5"));
    }

    /**
     * 查询计数
     * 审核状态，1为待审核，2为自动审核通过，3为待人工审核，4为人工审核拒绝，5为人工审核通过，6为自动审核拒绝
     *
     * @param code 1 全部 2 待审核 3 已通过 4 已拒绝
     * @return
     */
    @Override
    public Integer queryTotal(Integer code) {
        Integer count;
        switch (code) {
            case 1:
                count = publishInfoMapper.selectCount(Wrappers.lambdaQuery(PublishInfo.class));
                break;
            case 2:
                count = publishInfoMapper.selectCount(Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getState, "1", "3"));
                break;
            case 3:
                count = publishInfoMapper.selectCount(Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getState, "2", "5"));
                break;
            case 4:
                count = publishInfoMapper.selectCount(Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getState, "4", "6"));
                break;
            default:
                count = 0;
        }
        return count;
    }

    /**
     * 条件查询总条数
     *
     * @param code
     * @param likeId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public int queryCount(Integer code, Integer likeId, String startDate, String endDate) {
        Integer count;

        LambdaQueryWrapper<PublishInfo> queryWrapper = Wrappers.lambdaQuery(PublishInfo.class)
                .like(ObjectUtil.isNotNull(likeId), PublishInfo::getUserId, likeId)
                .gt(!StrUtil.equals("-1", startDate), PublishInfo::getCreated, Convert.toLong(startDate))
                .lt(!StrUtil.equals("-1", endDate), PublishInfo::getCreated, Convert.toLong(endDate));

        switch (code) {
            case 1:
                count = publishInfoMapper.selectCount(queryWrapper);
                break;
            case 2:
                count = publishInfoMapper.selectCount(queryWrapper.in(PublishInfo::getState, "1", "3"));
                break;
            case 3:
                count = publishInfoMapper.selectCount(queryWrapper.in(PublishInfo::getState, "2", "5"));
                break;
            case 4:
                count = publishInfoMapper.selectCount(queryWrapper.in(PublishInfo::getState, "4", "6"));
                break;
            default:
                count = 0;
        }
        return count;
    }

    /**
     * 人工审核通过 5
     *
     * @param ids
     */
    @Override
    public Integer passPublishByIds(List<String> ids) {
        return publishInfoMapper.update(PublishInfo.builder().state("5").updated(System.currentTimeMillis()).build(),
                Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getId, ids));
    }

    /**
     * 人工审核拒绝 4
     *
     * @param ids
     * @return
     */
    @Override
    public Integer rejectPublishByIds(List<String> ids) {
        return publishInfoMapper.update(PublishInfo.builder().state("4").updated(System.currentTimeMillis()).build(),
                Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getId, ids));
    }

    /**
     * 人工审核拒绝 3
     *
     * @param ids
     * @return
     */
    @Override
    public Integer revocationPublishByIds(List<String> ids) {
        return publishInfoMapper.update(PublishInfo.builder().state("3").updated(System.currentTimeMillis()).build(),
                Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getId, ids));
    }

    /**
     * 根据publishInfoId查询
     *
     * @param publishInfoId
     * @return
     */
    @Override
    public PublishInfo queryPublishInfoById(String publishInfoId) {
        return publishInfoMapper.selectById(publishInfoId);
    }

    /**
     * 查询所有选中的消息内容
     *
     * @param ids
     * @return
     */
    @Override
    public List<PublishInfo> queryPublishInfoByIds(List<String> ids) {
        return publishInfoMapper.selectList(Wrappers.lambdaQuery(PublishInfo.class).in(PublishInfo::getId, ids));
    }

    /**
     * 消息置顶 2
     *
     * @param publishInfoId
     * @return
     */
    @Override
    public Integer topPublishInfoById(String publishInfoId) {
        return publishInfoMapper.update(PublishInfo.builder().istop(2).updated(System.currentTimeMillis()).build(),
                Wrappers.lambdaQuery(PublishInfo.class).eq(PublishInfo::getId, publishInfoId));
    }

    /**
     * 消息取消置顶 top 1
     *
     * @param publishInfoId
     * @return
     */
    @Override
    public Integer unTopPublishInfoById(String publishInfoId) {
        return publishInfoMapper.update(PublishInfo.builder().istop(1).updated(System.currentTimeMillis()).build(),
                Wrappers.lambdaQuery(PublishInfo.class).eq(PublishInfo::getId, publishInfoId));
    }

    /**
     * 查全部publishInfo
     *
     * @return
     */
    @Override
    public List<PublishInfo> findAll() {
        return publishInfoMapper.selectList(Wrappers.lambdaQuery(PublishInfo.class));
    }
}
